﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace Informant.Clients.UI.Wpf4.Utilities
{
	/// <summary>
	/// Source= http://www.wpftutorial.net/PasswordBox.html
	/// </summary>
	public static class PasswordHelper
	{
		#region Password Dependency Property

		public static readonly DependencyProperty PasswordProperty =
			 DependencyProperty.RegisterAttached("Password",
			 typeof(String),
			 typeof(PasswordHelper),
			 new FrameworkPropertyMetadata(String.Empty, OnPasswordPropertyChanged));

		public static string GetPassword(DependencyObject dp)
		{
			return (string)dp.GetValue(PasswordProperty);
		}
		public static void SetPassword(DependencyObject dp, String value)
		{
			dp.SetValue(PasswordProperty, value);
		}
		
		/// <summary>
		/// Called when [password property changed].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
		private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;
			passwordBox.PasswordChanged -= PasswordChangedHandler;

			if (!GetIsUpdating(passwordBox))
			{
				passwordBox.Password = (String)e.NewValue;
			}
			passwordBox.PasswordChanged += PasswordChangedHandler;
		}

		#endregion

		#region Attach DependencyProperty

		public static readonly DependencyProperty AttachProperty =
			 DependencyProperty.RegisterAttached("Attach",
			 typeof(Boolean),
			 typeof(PasswordHelper),
			 new PropertyMetadata(false, Attach));

		public static void SetAttach(DependencyObject dp, Boolean value)
		{
			dp.SetValue(AttachProperty, value);
		}
		public static bool GetAttach(DependencyObject dp)
		{
			return (bool)dp.GetValue(AttachProperty);
		}

		/// <summary>
		/// Attaches or detaches the PasswordChangedHandler to the current passwordBox
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
		private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;

			if (passwordBox == null)
				return;

			if ((Boolean)e.OldValue)
			{
				passwordBox.PasswordChanged -= PasswordChangedHandler;
			}

			if ((Boolean)e.NewValue)
			{
				passwordBox.PasswordChanged += PasswordChangedHandler;
			}
		}
		/// <summary>
		/// Handles the PasswordChanged event of the PasswordBox.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private static void PasswordChangedHandler(Object sender, RoutedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;
			SetIsUpdating(passwordBox, true);
			SetPassword(passwordBox, passwordBox.Password);
			SetIsUpdating(passwordBox, false);
		}

		#endregion

		#region IsUpdating DependencyProperty

		private static readonly DependencyProperty IsUpdatingProperty =
			DependencyProperty.RegisterAttached("IsUpdating", typeof(Boolean),
			typeof(PasswordHelper));

		private static bool GetIsUpdating(DependencyObject dp)
		{
			return (bool)dp.GetValue(IsUpdatingProperty);
		}
		private static void SetIsUpdating(DependencyObject dp, Boolean value)
		{
			dp.SetValue(IsUpdatingProperty, value);
		}

		#endregion
	}
}
